/*
Class: PageLink
    Abstract class implements data exchange with RAP server to create link.

Example:
(start code)
    x = new Class({
        Implement: [Options, PageLink],
        initialize:function(...){
          ..
          this.initializePageLink( btns, 10 );
          ..
        },
        ..
        createLink: function(){ ... },
(end)
*/

!function() {
	/*
	 * Call server`s procedure which produce page link.
	 */
	function createLink(arguments) {

		var self = this;

        var txta = self.textarea;
        var selection = txta.getSelection();

		var remoteObject = rap.getRemoteObject(elwikiPageEditor);
		remoteObject.call("newLink", {"selection" : selection});
	}

	this.PageLink = new Class({

		initializeLinkUtils : function() {

			var self = this;
			var undo = self.undo = [];

			self.addEvents({
				//Persist the current state of the undo-able object.
				//Calls the {{this.getState()}} to retrieve the state info.
				//  state - (optional) state object to be persisted. If not present,
				//          the state will be retrieved via {{this.getState()}} function.
				createLink : createLink.pass(arguments, self),
			});
		},

		putPageLink : function(pageLink) {
			var self = this;
			var s = pageLink;

			var txta = self.textarea;
			var caret = txta.getSelectionRange();
			var caretStart = caret.start;

			var selectionLength = 0; // hasCaret ? s.length : 0;

			self.inject(s, caretStart, selectionLength);
			self.reset();

			self.fireEvent("change");
			//allow to finish the actions, before opening a new suggestion dialog
			self.suggest.delay(10, self);
		}
	});

}();
